opt_random := if os_family() != "windows" { if env("NO_WREN_RANDOM","0")!="0" {"-DWREN_OPT_RANDOM=0"} else{""} } else{""}
opt_meta := if os_family() != "windows" { if env("NO_WREN_META","0")!="0" {"-DWREN_OPT_RANDOM=0"} else{""} }else{""}
no_nan_tagging := env("NO_NAN_TAGGING","1")
target_arch := if env("TARGET_ARCH","x86_64") == "x86_64" {"64bit"} else {"32bit"}
default: lib
    cjpm build

[working-directory("./wren/projects/")]
[unix]
lib os="":
    #!/usr/bin/env bash
    if [[ "{{os}}" == "" ]]; then
        target_os="{{os()}}"
    elif [[ "{{os}}" == *bsd ]]; then
        target_os="bsd"
    else
        target_os="{{os}}"
    fi
    local_machine=""
    case $target_os in
    macos)
        if [[ "{{os()}}" == "macos" ]]; then
            local_machine="1"
        fi
        cd make.mac;;
    linux)
        if [[ "{{os()}}" == "linux" ]]; then
            local_machine="1"
        fi
        cd make;;
    bsd)
        if [[ "{{os()}}" =~ "bsd" ]]; then
            local_machine="1"
        fi
        cd make.bsd;;
    *)
        echo "${target_os} not supported"
        exit 1;;
    esac
    target_name="release_{{target_arch}}"
    # if [[ "{{no_nan_tagging}}" && "{{no_nan_tagging}}" != "0" ]]; then
    #     target_name="release_{{target_arch}}-no-nan-tagging"
    # fi
    echo "os: ${target_os}, target: ${target_name}"
    make wren config=$target_name
    make wren_shared config=$target_name
    if [[ "$local_machine" != "" ]]; then
        echo "copy lib to DYLD_LIBRARY_PATH"
        just fix
    fi
    

[windows]
[working-directory("./wren/projects/vs2019")]
lib target="64bit":
    #msbuild wren.sln /p:Configuration=Release /p:Platform={{target}}
fix:
    #!/usr/bin/env bash
    #fix for no static link
    hw_arch=$(uname -m)
    if [[ "$hw_arch" == "" ]]; then
        hw_arch="x86_64"
    elif [[ "$hw_arch" == "arm64" ]]; then
        hw_arch="aarch64"
    fi
    # echo "${CANGJIE_HOME}/runtime/lib/darwin_${hw_arch}_llvm/"
    cp wren/lib/lib*.dylib "${CANGJIE_HOME}/runtime/lib/darwin_${hw_arch}_llvm/"
test:
    WREN_LINK_OPTION=" -lwren -L./wren/lib " cjpm test
fmt:
    cjfmt -d src
clean:
    rm tmpFile*